<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 积分模板控制器
 * @package app\index\controller
 */
class ViperIntegral extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取积分模板列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $data = Db::name('qt_viper_integral_exchange')
                ->field('id,name,integral,money,goods_name,vip_type_id')
                ->where(['store_id' => $userInfo['store_id']])
                ->order('id desc')
                ->paginate($getData['limit'])->each(function ($item){
                    $viperIds = explode(',',$item['vip_type_id']);
                    $viperType = Db::name('qt_viper_type')->whereIn('id',$viperIds)->column('name');
                    $item['viper_type_name'] = implode(',',$viperType);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加积分模板
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\web\validate\ViperIntegral();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['create_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $insert = Db::name('qt_viper_integral_exchange')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存积分模板
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\web\validate\ViperIntegral();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $update= Db::name('qt_viper_integral_exchange')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存积分模板
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_viper_integral_exchange')
                ->field('id,name,integral,money,goods_name,vip_type_id')
                ->where('id',$getData['id'])->find();
            $viperIds = explode(',',$data['vip_type_id']);
            $viperType = Db::name('qt_viper_type')->whereIn('id',$viperIds)->column('name');
            $data['viper_type_name'] = implode(',',$viperType);
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除积分模板
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_viper_integral_exchange')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }






}
